package com.hm.service.config.db.ds1;

import com.hm.service.mapper.cron.CronExpressionMapper;
import com.hm.service.mapper.incident.CityCaseMapper;
import com.hm.service.mapper.log.TaskLogMapper;
import com.hm.service.mapper.recording.CasePushTimeMapper;
import com.hm.service.mapper.task.TaskDetailsMapper;
import com.hm.service.mapper.user.UserMapper;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SqlSessionFactoryConfig {
    private static final Logger logger = LoggerFactory.getLogger(SqlSessionFactoryConfig.class);

    @Bean(name = {"MainSqlSessionFactory"})
    public SqlSessionFactory sqlSessionFactory() {
        logger.warn("=======>MyBatisSessionFactory初始化了<=======");
        Environment environment = new Environment("development", new JdbcTransactionFactory(), new CustomHikariDataSource());
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration(environment);
        //开启驼峰规则
        configuration.setMapUnderscoreToCamelCase(true);
        //测试环境-开启日志
        configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
        //注册mapper
        mapperRegister(configuration);
        return new SqlSessionFactoryBuilder().build(configuration);
    }

    private void mapperRegister(org.apache.ibatis.session.Configuration configuration) {
        configuration.addMapper(CronExpressionMapper.class);
        configuration.addMapper(CityCaseMapper.class);
        configuration.addMapper(TaskLogMapper.class);
        configuration.addMapper(CasePushTimeMapper.class);
        configuration.addMapper(TaskDetailsMapper.class);
        configuration.addMapper(UserMapper.class);
    }


}
